package com.hunglv.transportalert;

import com.hunglv.databasehelper.SetAlarmLocationDatabaseHelper;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnTouchListener {
	// controls
	private ImageButton new_btn;

	// for later use
	private RelativeLayout layout_alarm;
	static final int CREATE_OR_DELETE_REQUEST = 0;

	// database helper
	protected static SetAlarmLocationDatabaseHelper setAlarmLocationDbHelper;

	// application resource
	protected static Resources mResources;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// get application resources
		mResources = this.getResources();

		new_btn = (ImageButton) findViewById(R.id.new_btn);
		new_btn.setOnTouchListener(this);

		setAlarmLocationDbHelper = new SetAlarmLocationDatabaseHelper(this);
		setAlarmLocationDbHelper.open();
	}

	@Override
	public void onResume() {
		super.onResume();
		Cursor allSetAlarmCursor = setAlarmLocationDbHelper.getAllCursor();
		if (allSetAlarmCursor.moveToFirst()) {
			do {
				int id = allSetAlarmCursor
						.getInt(SetAlarmLocationDatabaseHelper.COLUMN_ID);
				int stop_id = allSetAlarmCursor
						.getInt(SetAlarmLocationDatabaseHelper.COLUMN_STOP_ID);
				Log.v("alarm",
						String.format("id: %d, stop_id: %d", id, stop_id));
			} while (allSetAlarmCursor.moveToNext());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		setAlarmLocationDbHelper.close();
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (v.getId() == R.id.new_btn) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				Intent intent = new Intent(MainActivity.this,
						StopPickerActivity.class);
				startActivity(intent);
				return true;
			}
		}
		return false;
	}
}
